package com.weddingee.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class ProxoolDatabaseConnectionManager {

	public static Connection getConnection() {
		Connection connection = null;
		try {
			Class.forName("com.mysql.jdbc.Driver");

			connection = DriverManager
					.getConnection("proxool.db");
		} catch (Exception ex) {
			ex.printStackTrace();
		}
		return connection;
	}

	public static void release(Connection conn) {
		try {
			if (conn != null) {
				conn.close();
			}
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Connection conn = ProxoolDatabaseConnectionManager.getConnection();
		Statement stm = null;
		/*
		 * try { stm = conn.createStatement(); int res = stm.executeUpdate(
		 * "insert into news values(null,'daniel1','2012-06-07','hello,world1!','/test/test1.jpg')"
		 * );
		 * 
		 * System.out.println(res); } catch (SQLException e) {
		 * e.printStackTrace(); } finally{ try { stm.close(); conn.close(); }
		 * catch (SQLException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } }
		 */

		try {
			stm = conn.createStatement();
			ResultSet rs = stm.executeQuery("select * from news");
			while (rs.next()) {
				String name = rs.getString("name");
				System.out.println(name);
			}
		} catch (SQLException e) {
			try {
				stm.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}

		try {
			stm = conn.createStatement();
			ResultSet rs = stm.executeQuery("select * from news");
			while (rs.next()) {
				String name = rs.getString("ID");
				System.out.println(name);
			}
		} catch (SQLException e) {
			try {
				stm.close();
				conn.close();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
		}
	}
}
